from django.http import Http404, HttpResponse
from django.shortcuts import render_to_response, HttpResponse
from models import TextMessage, DLR_CODES
from signals import aql_sms_callback
from datetime import datetime

def process_aql_callback(request, sms_id):
    """"""
    rprt_code = int(request.GET['reportcode'])
    dst_num = request.GET['destinationnumber']
    try:
        text_message = TextMessage.objects.get(id=sms_id)
    except (KeyError, TextMessage.DoesNotExist):
        raise Http404
    
    if dst_num == text_message.tel_num:
        text_message.delivery_code = rprt_code
        text_message.date_callback = datetime.now()
        text_message.save()
        
        #probably a better way to translate this but this ok for now
        delivery = [dlr[1] for dlr in DLR_CODES if dlr[0] == text_message.delivery_code][0]
        aql_sms_callback.send(sender=text_message.__class__, instance=text_message, delivery_string=delivery)
        return HttpResponse('<h1>Thanks, please call again!</h1>')
    else:
        raise Http404
